package com.graden.background.service.sysuserlog.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graden.background.controller.sysuserlog.vo.UserLogVO;
import com.graden.background.common.enums.OperationEnum;
import com.graden.background.repository.sysuserlog.SysUserLogMapper;
import com.graden.background.repository.sysuserlog.entity.SysUserLog;
import com.graden.background.repository.sysuserlog.entity.SysUserLogDTO;
import com.graden.background.service.sysuserlog.ISysUserLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作日志服务实现类
 * </p>
 *
 * @author
 * @since 2019-03-28
 */
@Service
public class SysUserLogServiceImpl extends ServiceImpl<SysUserLogMapper, SysUserLog> implements ISysUserLogService {

	@Resource
	SysUserLogMapper sysUserLogMapper;

	/**
	 * 分页获取操作日志
	 */
	@Override
	public PageInfo getAllByPage(int pageNo, int pageSize, UserLogVO userLogVO) {
		PageHelper.startPage(pageNo, pageSize);

		List<SysUserLogDTO> list = sysUserLogMapper.findUserLog(userLogVO);;

		return new PageInfo<>(list);
	}

	/**
	 * 获取操作模板下拉框
	 */
	@Override
	public List<Map<String, Object>> getOperationMenu() {
		List<Map<String, Object>> list = new ArrayList<>();
		for (OperationEnum e : OperationEnum.values()) {
			Map<String, Object> map = new HashMap<>();
			map.put("menuId", e.getMenuId());
			map.put("menuName", e.getMenuName());
			list.add(map);
		}
		//去重复
		HashSet h = new HashSet(list);
		list.clear();
		list.addAll(h);
		return list.stream().sorted(Comparator.comparing(SysUserLogServiceImpl::comparingByMenuId)).collect(Collectors.toList());
	}

	private static int comparingByMenuId(Map<String, Object> map) {
		return (Integer) map.get("menuId");
	}

	/**
	 * 获取操作行为下拉框
	 */
	@Override
	public List<Map<String, Object>> getOperationFunction(Integer modelId) {
		List<Map<String, Object>> list = new ArrayList<>();
		for (OperationEnum e : OperationEnum.values()) {
			if (modelId == e.getMenuId() || modelId == null || modelId == 0) {
				Map<String, Object> map = new HashMap<>();
				map.put("functionId", e.getFunctionId());
				map.put("functionName", e.getFunctionName());
				list.add(map);
			}
		}
		return list.stream().sorted(Comparator.comparing(SysUserLogServiceImpl::comparingByFunctionId)).collect(Collectors.toList());
	}

	private static int comparingByFunctionId(Map<String, Object> map) {
		return (Integer) map.get("functionId");
	}
}
